package com.wk.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wk.sms.entity.SkuBounds;
import com.wk.sms.entity.SkuFullReduction;
import com.wk.sms.entity.SkuLadder;
import com.wk.sms.mapper.SkuBoundsMapper;
import com.wk.sms.mapper.SkuFullReductionMapper;
import com.wk.sms.mapper.SkuLadderMapper;
import com.wk.sms.service.SkuBoundsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wk.sms.vo.SaleVO;
import com.wk.sms.vo.SkuSaleVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class SkuBoundsServiceImpl extends ServiceImpl<SkuBoundsMapper, SkuBounds> implements SkuBoundsService {

    @Autowired
    private SkuLadderMapper skuLadderMapper;

    @Autowired
    private SkuFullReductionMapper fullMapper;

    //保存营销信息的3张表
    @Transactional
    @Override
    public void saveSaleAttr(SkuSaleVO skuSaleVO) {
        Long skuId = skuSaleVO.getSkuId();

        //1.sms_sku_bounds表
        SkuBounds skuBounds = new SkuBounds();
        //两个对象相同的属性直接赋值
        BeanUtils.copyProperties(skuSaleVO, skuBounds);
        //用二进制的4个1记录是否有优惠
        List<Integer> work = skuSaleVO.getWork();
        //四个二进制位拼接成字符串
        String workStr = StringUtils.join(work, "");
        //二进制转十进制
        skuBounds.setWork(Integer.parseInt(workStr,2));
        baseMapper.insert(skuBounds);

        //2.sms_sku_ladder表 打折信息
        SkuLadder skuLadder = new SkuLadder();
        skuLadder.setSkuId(skuId);
        skuLadder.setFullCount(skuSaleVO.getFullCount());       //满减信息
        skuLadder.setDiscount(skuSaleVO.getDiscount());         //折扣信息
        skuLadder.setAddOther(skuSaleVO.getLadderAddOther());   //是否叠加
        skuLadderMapper.insert(skuLadder);

        //3.sms_sku_full_reduction表 满减信息
        SkuFullReduction skuFullReduction = new SkuFullReduction();
        skuFullReduction.setSkuId(skuId);
        skuFullReduction.setFullPrice(skuSaleVO.getFullPrice());
        skuFullReduction.setReducePrice(skuSaleVO.getReducePrice());
        skuFullReduction.setAddOther(skuSaleVO.getFullAddOther());
        fullMapper.insert(skuFullReduction);

//        int i = 1/0;
    }

    //根据skuId查询营销信息
    @Override
    public List<SaleVO> querySaleVOBySkuId(Long skuId) {
        List<SaleVO> saleVOs = new ArrayList<>();

        //积分
        SkuBounds skuBounds = baseMapper.selectOne(new QueryWrapper<SkuBounds>().eq("sku_id", skuId));
        if (skuBounds != null) {
            SaleVO saleVO = new SaleVO();
            saleVO.setType("积分");

            StringBuilder builder = new StringBuilder();

            BigDecimal growBounds = skuBounds.getGrowBounds();
            BigDecimal buyBounds = skuBounds.getBuyBounds();

            if(growBounds!=null && growBounds.intValue()>0){
                builder.append("赠送成长值: ").append(growBounds);
            }

            if(buyBounds!=null && buyBounds.intValue()>0){
                builder.append("赠送积分: ").append(buyBounds);
            }

            saleVO.setDesc(builder.toString());
            saleVOs.add(saleVO);
        }

        //打折
        SkuLadder skuLadder = skuLadderMapper.selectOne(new QueryWrapper<SkuLadder>().eq("sku_id", skuId));
        if (skuLadder != null) {
            SaleVO saleVO = new SaleVO();
            saleVO.setType("折扣");
            saleVO.setDesc("满"+skuLadder.getFullCount()+"件, 打"+skuLadder.getDiscount().divide(new BigDecimal(10))+"折");
            saleVOs.add(saleVO);
        }

        //满减
        SkuFullReduction reduction = fullMapper.selectOne(new QueryWrapper<SkuFullReduction>().eq("sku_id", skuId));
        if (reduction != null) {
            SaleVO saleVO = new SaleVO();
            saleVO.setType("满减");
            saleVO.setDesc("满"+reduction.getFullPrice()+"减"+reduction.getReducePrice());
            saleVOs.add(saleVO);
        }

        return saleVOs;
    }

}
